/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.youfood.web.servlet.admin.employe;

import com.supinfo.youfood.entity.Employe;
import com.supinfo.youfood.entity.Restaurant;
import com.supinfo.youfood.entity.TypeEmploye;
import com.supinfo.youfood.service.EmployeService;
import com.supinfo.youfood.service.RestaurantService;
import com.supinfo.youfood.service.TypeEmployeService;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 *
 * @author TaKeO
 */
@WebServlet(name = "AddEmployeServlet", urlPatterns = {"/admin/ajouter-employe"})
public class AddEmployeServlet extends HttpServlet {
    
    @EJB
    private TypeEmployeService typeEmployeService;
    
    @EJB
    private RestaurantService restoService;
    
    @EJB
    private EmployeService employeService;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        Employe currentEmploye = (Employe) request.getSession().getAttribute("currentEmp");
        
        if(request.getParameter("id")!=null)
        {
            Employe employe = employeService.findEmployeById(new Long(request.getParameter("id")));
            request.setAttribute("employe", employe);
        }
        
        List<TypeEmploye> types;        
        if(currentEmploye.getType_employe().getDroits() < 700)
        {
            types = typeEmployeService.getGerantTypeEmploye();
        }
        else
        {
            types = typeEmployeService.getAllTypeEmploye();
        }
        request.setAttribute("types", types);        
        
        List<Restaurant> restos;
        if(currentEmploye.getType_employe().getDroits() < 700)
        {
            restos = new ArrayList();
            restos.add(currentEmploye.getResto());
        }
        else
        {
            restos = restoService.getAllResto();
        }
        
        request.setAttribute("restos", restos);
        
        request.getRequestDispatcher("/jsp/admin/ajouter-employe.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        Employe employe;
        if(request.getParameter("id")!=null)
        {
            employe = employeService.findEmployeById(new Long(request.getParameter("id")));
        }
        else
        {
            employe = new Employe();
        }
        
        employe.setNom(request.getParameter("nom"));
        employe.setPrenom(request.getParameter("prenom"));
        
        try
        {
            employe.setCode_acces(new Integer(request.getParameter("code")));
        }
        catch(Exception e){ /*erreur*/ }
        
        TypeEmploye type = typeEmployeService.findTypeEmployeById(new Long(request.getParameter("type")));
        employe.setType_employe(type);
        Restaurant resto = restoService.findRestoById(new Long(request.getParameter("resto")));
        employe.setResto(resto);
        
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();

        Set<ConstraintViolation<Employe>> constraintViolations = validator.validate(employe);

        if (constraintViolations.size() > 0) 
        {
            List<String> errors = new ArrayList<String>();
            
            for (ConstraintViolation<Employe> contraintes : constraintViolations) 
            {
                errors.add(contraintes.getPropertyPath()+" "+contraintes.getMessage());
            }
            
            request.setAttribute("errors", errors);
            getServletContext().getRequestDispatcher("/jsp/admin/ajouter-employe.jsp").forward(request, response);
        }
        else
        {
            if(request.getParameter("id")!=null)
            {
                employe = employeService.refreshEmploye(employe);
            }
            else
            {
                employe = employeService.addEmploye(employe);
            }

            resto.addEmploye(employe);
            restoService.refreshResto(resto);
            type.addEmploye(employe);
            typeEmployeService.refreshTypeEmploye(type);

            response.sendRedirect("/YouFood/admin/liste-employes");
        }
    }

}
